package cn.com.infcn.monitor.server;

import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置文件
 * @author jijs
 *
 */
public class IMonitorConfig {
	
	private static Logger logger = LoggerFactory.getLogger(IMonitorConfig.class);
	
	private final static Properties rnsProps = new Properties();
	private static IMonitorConfig instance;
	
	private IMonitorConfig(String conf) {
		InputStream input = this.getClass().getResourceAsStream(conf);
		try {
			rnsProps.load(input);
		} catch (Exception e) {
			logger.error("config file: {} not found, exception:{}", conf, e);
		}
	}

	public static IMonitorConfig getInstance() {
		if (instance == null) {
			instance = new IMonitorConfig("/monitor.conf");
		}
		return instance;
	}
	
	public String getStringValue(String key) {
		return rnsProps.getProperty(key);
	}
	
	public String getStringValue(String key, String defaultValue) {
		String value = rnsProps.getProperty(key);
		if (value == null) {
			value = defaultValue;
		}
		return value;
	}
	
	public int getIntValue(String key) {
		String value = rnsProps.getProperty(key);
		return Integer.parseInt(value);
	}
	
	public int getIntValue(String key, int defaultValue) {
		String value = rnsProps.getProperty(key);
		int v = defaultValue;
		try {
			v = Integer.parseInt(value);
		} catch (Exception e) {
			logger.error(key+" value {} is not number", value);
		}
		
		return v;
	}
	
	public boolean getBooleanValue(String key) {
		String value = rnsProps.getProperty(key);
		return Boolean.parseBoolean(value);
	}
	
	public boolean getBooleanValue(String key, boolean defaultValue) {
		String value = rnsProps.getProperty(key);
		boolean b = defaultValue;
		if (value != null) {
			b = Boolean.parseBoolean(value);
		}
		return b;
	}

}
